package com.sree.tali.actions;

import com.sree.tali.buscomp.DisplayBusComp;
import com.sree.tali.buscomp.DisplayBusCompImpl;
import com.sree.tali.buscomp.QuestionBusComp;
import com.sree.tali.buscomp.QuestionBusCompImpl;
import com.sree.tali.hibernate.pojo.QuestionPojo;
import com.sree.tali.plugin.HibernatePlugin;
import com.sree.tali.pojo.AdminHomepojo;
import com.sree.tali.pojo.BitForm;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.actions.DispatchAction;
import org.hibernate.SessionFactory;

public class Question
  extends DispatchAction
{
  public ActionForward addBit(ActionMapping paramActionMapping, ActionForm paramActionForm, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    BitForm bitform = (BitForm)paramActionForm;
    QuestionBusComp questionbuscomp = new QuestionBusCompImpl();
    SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
    QuestionPojo questionpojo = new QuestionPojo();
    copyFormToQuestionVo(bitform,questionpojo);
    questionbuscomp.addBit(questionpojo, localSessionFactory.openSession());
//localDisplayBusCompImpl.DisplayHome(localSessionFactory.openSession(), paramHttpServletRequest);
    return paramActionMapping.findForward("success");
  }
  public ActionForward showBit(ActionMapping paramActionMapping, ActionForm paramActionForm, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String category = paramHttpServletRequest.getParameter("category");
    System.out.print("category is =============================== " + category);
    QuestionBusComp questionbuscomp = new QuestionBusCompImpl();
    SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
    List<QuestionPojo> questionpojolist = questionbuscomp.showbits(category, localSessionFactory.openSession());
    System.out.println(questionpojolist);
    paramHttpServletRequest.getSession().setAttribute("questionpojolist", questionpojolist);
    //System.out.println("Size is =================  "+questionpojolist.get(0).getAnswers().size());
    return paramActionMapping.findForward("bitpage");
  }
  public ActionForward updateBit(ActionMapping paramActionMapping, ActionForm paramActionForm, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    DisplayBusCompImpl localDisplayBusCompImpl = new DisplayBusCompImpl();
    SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
    localDisplayBusCompImpl.DisplayHome(localSessionFactory.openSession(), paramHttpServletRequest);
    return paramActionMapping.findForward("homepage");  
  }
  
    public void copyFormToQuestionVo(BitForm bitform,QuestionPojo questionpojo)
    {
        questionpojo.setAnswer(bitform.getAnswer());
        questionpojo.setCategory(bitform.getCategory());
        questionpojo.setDescription(bitform.getDescription());
        questionpojo.setOptions(bitform.getOptions());
        questionpojo.setQuestion(bitform.getQuestion());
        questionpojo.setStatus("INACTIVE");
    }
}